<?php
include_once 'Node.php';

class SimpleInterpreter{
	public function interpret(Node &$node, $intent = 0){				
		$name = $node->getAttribute('#');
		$childs = $node->getChilds();
		
		if($name != ''){
			$ret = $this->getIntent($intent).$name;
		}else{
			$ret = $this->getIntent($intent);
		}
		
		$hasAttr = false;	
		for($i=0; $i<$node->atidx; ++$i){
			if(!$hasAttr){
				$hasAttr = true;
				$ret .= '(';
			}
			
			$ret .= '"'.$node->attrs['@'.$i].'", ';
		}
		
		foreach ($node->attrs as $k => $v){
			if(!in_array($k[0], array('#', '>', '@'))){
				if(!$hasAttr){
					$hasAttr = true;
					$ret .= '(';
				}
				$ret .= $k.'="'.$v.'", ';
			}
		}
		
		if($hasAttr){
			$ret .= ')';
		}
		
		$hasChild = false;		
		foreach ($childs as $child){
			if(!$hasChild){
				$ret .= '{'.chr(10);
				$hasChild = true;
			}
			$ret .= $this->interpret($child, $intent+1);
		}		
		if($hasChild){
			$ret .= $this->getIntent($intent).'}';
		}
		$ret.=chr(10);
		
		return $ret;
	}
	
	function getIntent($intent=0){
		$ret ='';
		while($intent-->0){
			$ret .= '    ';
		}
		return $ret;
	}
}